/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ticketmaster.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.RowSet;
import ticketmaster.dataobject.Reward;

/**
 *
 * @author Wei Liu
 */
public class RewardDAO extends BaseEntityDAO
{

    public RewardDAO()
    {
        // initialization
    }

    /**
     * Insert reward into database
     * @param reward the new reward entity to be inserted
     * @return newly created reward number or a 0 on error
     */
    public int insertReward(Reward reward)
    {
        return save(reward);
    }

    /**
     * Delete reward that matches the supplied criteria
     * @param reward the supplied criteria
     * @return true on success, false on failure
     */
    public boolean deleteReward(Reward reward)
    {
        return delete(reward);
    }

    /**
     * Update the reward using data from the reward Data Object
     * @param reward the reward Data Object to be updated
     * @return true on success, false on failure or error
     */
    public boolean updateReward(Reward reward)
    {
        if (reward.getRewardID() == 0)
        {
            return false;
        }

        if (update(reward))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * Get the reward that matches the supplied criteria
     * @param reward the supplied criteria
     * @return a Data Object if found, or null on error or if not found
     */
    public Reward findReward(Reward reward)
    {
        String sql = buildQuerySQL(reward);
/*
        if (reward.getRewardID() != 0)
        {
            sql = buildQuerySQL(reward);
        }
        else
        {
            return null;
        }
*/
        RowSet crs = query(sql);
        try
        {
            crs.next();
        }
        catch (SQLException ex)
        {
            Logger.getLogger(RewardDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return (Reward) this.getObject(crs, reward);
    }

    /**
     * Get all rewards that match the supplied criteria
     * @param reward supplied criteria
     * @return a RowSet
     */
    public RowSet getRewardsRS(Reward reward)
    {
        String sql = buildQuerySQL(reward);
        return query(sql);
    }

    /**
     * Get all rewards that match the supplied criteria
     * @param reward supplied criteria
     * @return a collection of Reward Data Objects
     */
    public Collection<Reward> getRewardsDO(Reward reward)
    {
        String sql = buildQuerySQL(reward);

        RowSet crs = query(sql);
        Collection<Reward> rewards = new ArrayList<Reward>();
        try
        {
            while (crs.next())
            {
                Reward rewardDO = (Reward) this.getObject(crs, reward);
                Reward item = new Reward();
                item.setRewardID(rewardDO.getRewardID());
                item.setName(rewardDO.getName());
                item.setPoints(rewardDO.getPoints());
                item.setPrice(rewardDO.getPrice());
                rewards.add(item);
            }
        }
        catch (SQLException ex)
        {
            Logger.getLogger(RewardDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        return rewards;
    }
}
